

#import "NSURLSession+cagegory.h"
@implementation NSURLSession (cagegory)
+ (NSURLSession *)sessionWithDelegate:(id)Obj
{
    __weak __typeof__(Obj) weakObj = Obj;
    return [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:weakObj delegateQueue:[[NSOperationQueue alloc] init]];
}

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSMutableURLRequest *)request para:(NSDictionary *)para data:(NSArray < NSData *>*)datas
{
    request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
    
    NSMutableData *bodyData = [NSMutableData data];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMddhhmmssSSS"];
    NSString *bundary = [formatter stringFromDate:[NSDate date]];
    
    // 设置ContentType
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=Boundary+%@",bundary] forHTTPHeaderField:@"Content-Type"];
    
    // 设置文本参数
    [para enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
        [bodyData appendData:[[NSString stringWithFormat:@"--Boundary+%@\r\n", bundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:[[NSString stringWithFormat:@"%@\r\n", value] dataUsingEncoding:NSUTF8StringEncoding]];
    }];
    
    // 设置上传的NSData
    [datas enumerateObjectsUsingBlock:^(NSData * _Nonnull data, NSUInteger idx, BOOL * _Nonnull stop) {
        [bodyData appendData:[[NSString stringWithFormat:@"--Boundary+%@\r\n", bundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.png\"\r\n", bundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:[@"Content-Type: image/jpeg/png/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [bodyData appendData:data];
        [bodyData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }];
    
    [bodyData appendData:[[NSString stringWithFormat:@"--Boundary+%@--\r\n", bundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    /*开始上传*/
    return [self uploadTaskWithRequest:request fromData:bodyData];
}
/// 上传文件,base64
- (NSURLSessionUploadTask *)uploadBase64WithRequest:(NSMutableURLRequest *)request para:(NSDictionary *)para data:(NSArray < NSData *>*)datas
{
    NSMutableData *bodyData = [NSMutableData data];
    
    // 设置ContentType
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    // 设置上传的NSData
    if (datas == nil){
        NSData *datanew = [NSJSONSerialization dataWithJSONObject:para options:NSJSONWritingPrettyPrinted error:nil];
        [bodyData appendData:datanew];
    }else{
        [datas enumerateObjectsUsingBlock:^(NSData * _Nonnull data, NSUInteger idx, BOOL * _Nonnull stop) {
            [bodyData appendData:data];
        }];
    }
    return [self uploadTaskWithRequest:request fromData:bodyData];
}
@end
